<template>
	<view class="content">
		<!--  -->
		<uni-popup :show="type === 'code'" position="middle" mode="fixed"  style="position: relative;">
			<view class="uni-tip">
				<view class="codeitem" style="position: relative;z-index: 10;">
					<view style="padding:0 30upx;" class="flex aic jsb">
						<view class="flex aic">
							<text style="font-size: 30upx;color: #333;">获取</text>
							<text class="choosetxt choosen">{{filename}}</text>
						</view>
					</view>
				</view>
				<view class="codeitem" style="font-size: 30upx;color: #333;z-index:8;text-align: center;margin-top: 22upx;">
					<input type="number" placeholder="输入数量" v-model="filenum" style="color:#FF4242;font-size: 30upx;font-weight: 600;">
				</view>
				<view  @click="confirmfile()" class="getcode">确定</view>
			</view>
		</uni-popup>
		<view class="pageHeader">
			收取
			<view class="generalBack" @click="backpage()">
			 <image src="/static/b-1.png" style="width: 18upx;height: 30upx;"></image>
			 </view>
			 <view class="code" @click="saoma">
				 <image src="../../static/exchange/code.png" style="width: 26px;height:26px;"></image>
			 </view>
		</view>
		<view class="regoodbox">
			<view class="codebox">
				<view class="codetitle">扫二维码获取文件</view>
				<view class="codecont">
					<view class="borderin topleft"></view>
					<view class="borderin topright"></view>
					<view class="borderin btmleft"></view>
					<view class="borderin btmright"></view>
					<tki-qrcode ref="qrcode" :val="val" :size="size" :unit="unit"  :loadMake="loadMake"/>
				</view>
				<view style="margin-top: 50upx;">
					<view class="codeadd">文件地址</view>
					<view class="codeaddress">{{fileaddress}}</view>
				</view>
			</view>
		</view>
		<view class="customer">
			<view class=" flex aic jsb" style="padding:0 92upx;height: 100%;"> 
				<view class="flex aic"  @tap="shareWeiXin('WXSceneSession')">
					<image src="../../static/exchange/s-1.png" style="width: 40upx;height: 40upx;margin-right: 8upx;"></image>
					<text>分享</text>
				</view>
				<view class="flex aic"  @click="copytxt()">
					<image src="../../static/exchange/s-2.png"  style="width: 40upx;height: 40upx;margin-right: 8upx;"></image>
					<text>复制</text>
				</view>
				<image src="../../static/exchange/s-3.png" style="width: 40upx;height: 40upx;"></image>
			</view>
		</view>
	</view>
	</template>
	<script>
	import postAjax from '../../API/postAjax.js'
	import tkiQrcode from '../../components/tki-qrcode/tki-qrcode.vue'
	import uniPopup from '../../components/uni-popup/uni-popup.vue'
	export default {
		data() {
			return {
				userToken:'',// token
				transfile:{},
				// 文件夹地址
				fileaddress:'',
				type:'code',
				filenum:null,
				filename:'',
				myfilenumber:0,
				code:'',
				val:'file',//二维码的信息
				// 要生成的二维码值
				size: 440, // 二维码大小
				unit: 'upx', // 单位
				loadMake: true, // 组件加载完成后自动生成二维码
			}
		},
		async onLoad(options) {
			let that = this
			that.userToken = uni.getStorageSync('token')
			that.transfile = JSON.parse(options.item)
			that.filename = that.transfile.code
			that.myfilenumber = that.transfile.file_num
			let oinfo = await that.getUserInfo()
			that.fileaddress = oinfo.user.file_address
			that.val = that.fileaddress
		},
		components:{
			tkiQrcode,
			uniPopup
		},
		
		methods: {
			// 返回上一页
			backpage(){
				uni.navigateBack({
					delta:1
				})
			},
			// 复制文本
			copytxt(){
				uni.setClipboardData({
				    data:this.fileaddress,
				    success: function () {
				       uni.showToast({
				         title:'复制成功',
				         icon:'none'
				       })
				    }
				});
			},
			getUserInfo() {
				let that = this
				return new Promise((resolve, reject) => {
					postAjax('user/Center', {
						token: that.userToken
					}, function(data) {
						resolve(data.data);
					})
				})
			},
			togglePopup(type) {
				this.type = type;
			},
			confirmfile(){
				let that = this
				
				if(!that.filenum){
					uni.showToast({
						title:'请输入文件数量',
						icon:'none'
					})
					return
				}
				if(that.filenum>Number(that.myfilenumber)){
					uni.showToast({
						title:'您的'+that.filename+'不足',
						icon:'none'
					})
					return
				}
				that.val = 'add='+that.fileaddress+'&id='+that.transfile.id+'&num='+that.filenum+'&filename='+that.filename
				that.type = ''
			},
			shareWeiXin(scene){
				let _this = this
					let sc = scene
					_this.capture(sc)
			},
			capture(scene) {
			  let _this = this
			  let pages = getCurrentPages();
			  let page = pages[pages.length - 1];
			  console.log(pages)
			  let bitmap=null;  
			  let currentWebview = page.$getAppWebview();    
			  bitmap = new plus.nativeObj.Bitmap('amway_img');  
			  // 将webview内容绘制到Bitmap对象中  
			  currentWebview.draw(bitmap,function(){  
			      console.log('截屏绘制图片成功');  
			      // bitmap.save( "_doc/"+Math.random()+".jpg"
			      bitmap.save( "_doc/invite.jpg"
			      ,{}  
			      ,function(i){  
			          console.log('保存图片成功：'+JSON.stringify(i));
			          uni.saveImageToPhotosAlbum({  
			              filePath: i.target,  
			              success: function () {
							_this.path = i.target
			                  bitmap.clear(); //销毁Bitmap图片  
			                  uni.showToast({  
			                      title: '保存图片成功',  
			                      mask: false,  
			                      duration: 1500  
			                  }); 
							uni.share({
								provider: "weixin",
								scene: scene,
								type: 2,
								imageUrl : _this.path,
								success: function (res) {
									console.log("success:" + JSON.stringify(res));
								},
								fail: function (err) {
									console.log("fail:" + JSON.stringify(err));
								}
							});
			              } 
			          });  
			      }  
			      ,function(e){  
			          console.log('保存图片失败：'+JSON.stringify(e));  
			      });  
			  },function(e){  
			      console.log('截屏绘制图片失败：'+JSON.stringify(e));  
			  });  
			  //currentWebview.append(amway_bit);    
			},
			saoma(){
				uni.scanCode({
				    onlyFromCamera: true,
				    success: function (res) {
				        console.log('条码类型：' + res.scanType);
				        console.log('条码内容：' + res.result);
						uni.navigateTo({
							url:'transout?status=1&'+res.result
						})
				    }
				});
			}
		}
	}
	
</script>

<style>
	.content{min-height: 100vh;background-color: #fff;}
	.pageHeader{position: fixed;top:0;left:0;z-index:999;width: 100%;height: 128upx;background-color: #1D84E8;line-height: 128upx;text-align: center;font-size: 36upx;color: #fff;padding-top: 40upx;}
	.generalBack{position: absolute;top:40upx;left:30upx;display: block;}
	.code{position: absolute;padding:10upx 20upx;top:36upx;right:20upx}
	.regoodbox{padding: 180upx 30upx 110upx;}
	.codebox{margin-top: 63upx;padding: 94upx;border:4upx solid #CBE5FF;border-radius:20upx;text-align: center;}
	.codetitle{font-size: 42upx;color: #666;}
	.codecont{width: 440upx;height: 440upx;padding: 20upx;position: relative;margin-top: 36upx;border-radius: 12upx;}
	.codeadd{font-size: 32upx;color: #666;}
	.codeaddress{font-size: 32upx;color: #EC6104;margin-top: 30upx;}
	.borderin{width: 88upx;height: 88upx;position: absolute;}
	.topleft{top:0;left:0;border-top:6upx solid #1D84E8;border-left: 6upx solid #1D84E8;border-radius: 16upx 0 0 0 ;}
	.topright{top:0;right:0;border-top:6upx solid #1D84E8;border-right: 6upx solid #1D84E8;border-radius:0 16upx 0 0 ;}
	.btmleft{bottom:0;left:0;border-bottom:6upx solid #1D84E8;border-left: 6upx solid #1D84E8;border-radius:0 0 0 16upx;}
	.btmright{bottom:0;right:0;border-bottom:6upx solid #1D84E8;border-right: 6upx solid #1D84E8;border-radius:0 0 16upx 0;}
	.customer{position: fixed;width: 100%;left: 0;bottom: 0;font-size: 28upx;color: #666;z-index:100;height: 110upx;background-color: #F9F9F9;}
	
	.codeitem{padding: 20upx 0;margin: 0 40upx;border:2upx solid #CBE5FF;border-radius: 6upx;}
	.getcode{width: 170upx;height: 66upx;line-height: 66upx;background-color: #1D84E8;border-radius: 6upx;color: #fff;font-size: 30upx;margin: 40upx auto 0;text-align: center;}
	.choosetxt{font-size: 26upx;color: #999;display: inline-block;width: 306upx;text-align: center;border-bottom:1px solid #707070;padding: 8upx 0;margin-left: 6upx;}
	.filebox{position: absolute;right:0;top:96upx;font-size: 28upx;color: #666;width:200upx;box-shadow:0px 4upx 8upx rgba(203,229,255,1);;background-color: #fff;}
	.filebox>.fileitem:not(:last-child){border-bottom: 2upx solid #eee;}
	.fileitem{margin: 0 12upx;padding: 14upx 0;}
	.grey{width: 30upx;height: 30upx;background-color: #DDDDDD;margin-right: 18upx;}
	.choosen{color: #EC6104;font-weight: 600;}
	.empty {
		position: fixed;
		left: 0;
		top: 0;
		width: 100%;
		height: 100vh;
		padding-bottom: 100upx;
		display: flex;
		justify-content: center;
		flex-direction: column;
		align-items: center;
		background: #fff;
	
	}
	
	.empty image {
		width:240upx;
		margin-bottom: 30upx;
	}
	.empty-tips {
		display: flex;
		font-size: 30upx;
		color: #666;
	
	}
	.uni-tip {
		/* #ifndef APP-NVUE */
		display: flex;
		flex-direction: column;
		/* #endif */
		padding: 36upx 0;
		width: 300px;
		background-color: #fff;
		border-radius: 10px;
	}
	
	
	. {
		
	}
</style>
